package dao.impl;


import bean.dto.VehicleChangeData;
import bean.pojo.ParkingSpace;
import bean.pojo.User;
import bean.vo.ParkingInfo;
import commons.dbHelper.DBHelper;
import dao.ParkingSpaceDao;

import java.util.ArrayList;
import java.util.List;

/**
 * bean.pojo.vehicle
 * User: 坚持
 * Date: 2024/12/18 11:34
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */

public class ParkingSpaceDaoImpl implements ParkingSpaceDao {


    @Override
    public Integer updateParkingSpace(VehicleChangeData vehicleChangeData) {
        String sql = " update parking_space set owner_vehicle_id=?,parking_vehicle_id=?,start_time=? where parking_space_id = ? ";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,vehicleChangeData.getOwnerVehicleId(), vehicleChangeData.getVehicleId(),
                vehicleChangeData.getStartTime(),vehicleChangeData.getParkingSpaceId());
    }

    @Override
    public Integer update(ParkingSpace parkingSpace) {
        String sql = " update parking_space set owner_vehicle_id=?,parking_vehicle_id=?,start_time=?  where parking_space_id = ? ";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,parkingSpace.getOwnerVehicleId(),parkingSpace.getParkingVehicleId(),
                parkingSpace.getStartTime(),parkingSpace.getParkingSpaceId());
    }

    @Override
    public Long findByTotal(Integer ownerVehicleId,Integer parkingVehicleId) {
        StringBuffer sql = new StringBuffer("select ps.parking_space_id,ps.owner_vehicle_id,ve.vehicle_license_plate,ps.parking_vehicle_id," +
                "ps.start_time from parking_space ps join vehicle ve on ps.owner_vehicle_id = ve.vehicle_id  ");
        List<Object>  parameters=new ArrayList<>();
        if (ownerVehicleId != null) {
            sql.append(" and ps.owner_vehicle_id = ? ");
            parameters.add(ownerVehicleId);
        }
        if (parkingVehicleId != null) {
            sql.append(" and ps.parking_vehicle_id = ? ");
            parameters.add(parkingVehicleId);
        }
        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        List<ParkingInfo> query = dbHelper.getBeanList(ParkingInfo.class,sql.toString(),parameters.toArray());
        if (query!=null){
            return (long)query.size();
        }
        return 0L;
    }

    @Override
    public List<ParkingInfo> findBy(Integer page, Integer limit,Integer ownerVehicleId,Integer parkingVehicleId) {
        StringBuffer sql = new StringBuffer("select ps.parking_space_id,ve.vehicle_license_plate,ps.owner_vehicle_id,ps.parking_vehicle_id" +
                ",ps.start_time from parking_space ps join vehicle ve on ps.owner_vehicle_id = ve.vehicle_id  ");
        List<Object>  parameters=new ArrayList<>();

        if (ownerVehicleId != null) {
            if (ownerVehicleId==0){
                sql.append(" and ps.owner_vehicle_id = 0 ");
            }else {
                sql.append(" and ps.owner_vehicle_id != 0 ");
            }
        }
        if (parkingVehicleId != null) {
            if (parkingVehicleId==0){
                sql.append(" and ps.parking_vehicle_id = 0 ");
            }else {
                sql.append(" and ps.parking_vehicle_id != 0 ");
            }
        }

        sql.append(" order by ps.parking_space_id asc ");
        sql.append(" limit " + (page - 1) * limit + "," + limit);
        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        return dbHelper.getBeanList(ParkingInfo.class,sql.toString(),parameters.toArray());
    }
}
